/**
 * @Project Name:blog-business-service
 * @Package Name:cn.hoofungson.blog.business.service.provider.convert.article
 * @Since JDK 1.8
 */

package cn.hoofungson.blog.business.service.provider.converter.article;

import cn.hoofungson.blog.business.service.interfaces.domain.dto.article.ArticleDTO;
import cn.hoofungson.blog.business.service.interfaces.domain.dto.channel.ChannelDTO;
import cn.hoofungson.blog.business.service.provider.model.entity.article.ArticlePO;
import cn.hoofungson.blog.business.service.provider.model.entity.channel.ChannelPO;
import org.springframework.cglib.beans.BeanCopier;

/**
 * @Description: ArticleConverter
 * @Author 胡松 hoofungson@163.com
 * @Date 2020-03-25 8:35
 * @Version V1.0
 */
public class ArticleConverter {

    /**
     * DTO转PO
     *
     * @param articleDTO
     * @param articlePO
     **/
    public static void convert(ArticleDTO articleDTO, ArticlePO articlePO) {
        if (null == articleDTO) {
            return;
        }
        if (null == articlePO) {
            return;
        }
        BeanCopier.create(articleDTO.getClass(), articlePO.getClass(), false)
                .copy(articleDTO, articlePO, null);
        if (null == articleDTO.getChannelDTO()) {
            return;
        }
        ChannelDTO channelDTO = articleDTO.getChannelDTO();
        ChannelPO channelPO = new ChannelPO();
        BeanCopier.create(channelDTO.getClass(), channelPO.getClass(), false)
                .copy(channelDTO, channelPO, null);
        articlePO.setChannelPO(channelPO);
    }

    /**
     * PO转DTO
     *
     * @param articlePO
     * @param articleDTO
     **/
    public static void convert(ArticlePO articlePO, ArticleDTO articleDTO) {
        if (null == articlePO) {
            return;
        }
        if (null == articleDTO) {
            return;
        }
        BeanCopier.create(articlePO.getClass(), articleDTO.getClass(), false)
                .copy(articlePO, articleDTO, null);
        if (null == articlePO.getChannelPO()) {
            return;
        }
        ChannelPO channelPO = articlePO.getChannelPO();
        ChannelDTO channelDTO = new ChannelDTO();
        BeanCopier.create(channelPO.getClass(), channelDTO.getClass(), false)
                .copy(channelPO, channelDTO, null);
        articleDTO.setChannelDTO(channelDTO);
    }
}
